package com.tiancheng.onecode.utils;

import com.tiancheng.onecode.exception.Asserts;
import com.tiancheng.onecode.exception.ErrorCodeException;
import com.tiancheng.onecode.trade.model.SettlementRuleResVO;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @Author: kellen
 * @Description: 清分信息计算工具类
 * @Date: create in 2024/12/18 14:24
 */
@Slf4j
@Data
public class ClearingComputeUtils {

    /**
     * 计算佣金
     *
     * @return
     */
    public static Integer platformCommission(Integer subAmount, SettlementRuleResVO ruleVO) {
        // 查无清分规则
        Asserts.isNotNull(ruleVO,"未查询到商户清分规则！！");
        // 自营、平台默认平台抽佣金额为0
        int platformAmount = 0;
        if (SettlementRuleResVO.SETTLEMENT_TYPE_CLEAR.equals(ruleVO.getSettlementType())) {
            if (SettlementRuleResVO.CLEARING_RULE_RATE.equals(ruleVO.getClearingRule())) {
                // 百分比
                platformAmount = ruleVO.getPlatformPre().multiply(new BigDecimal(subAmount)).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP).intValue();
            } else if (SettlementRuleResVO.CLEARING_RULE_NUM_P.equals(ruleVO.getClearingRule())) {
                // 平台固定金额
                // platformAmount = ruleVO.getPlatformFix() * productCount;
                throw new ErrorCodeException("暂不支持【平台固定金额】抽佣");
            } else if (SettlementRuleResVO.CLEARING_RULE_NUM_M.equals(ruleVO.getClearingRule())) {
                // 商户固定金额
                //platformAmount = subAmount - (ruleDTO.getMerchantFix() * productCount);
                throw new ErrorCodeException("暂不支持【商户固定金额】抽佣");
            } else {
                // 清分规则异常
                throw new ErrorCodeException("清分规则异常！");
            }
        }
        if (platformAmount < 0) {
            platformAmount = 0;
        }
        return platformAmount;
    }
}
